﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AteCapybara
{
    /// <summary>
    /// 存放扩展方法的类
    /// </summary>
    public static class Extend
    {
        /// <summary>
        /// 复制一个新的字典
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> Copy<TKey, TValue>(this Dictionary<TKey, TValue> dic)
        {
            Dictionary<TKey, TValue> dic2 = new Dictionary<TKey, TValue>();
            foreach (var key in dic.Keys)
            {
                dic2[key] = dic[key];
            }
            return dic2;
        }

        /// <summary>
        /// Json文件中的str转成int，并去除其中的,分隔符
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static int JsonToInt32(this string para)
        {
            try
            {
                return Convert.ToInt32(para.Trim().Replace(",", ""));
            }
            catch
            {
                throw new Exception($"[{para}]转换int类型失败！");
            }
        }

        public static double JsonToDouble(this string para)
        {
            try
            {
                return Convert.ToDouble(para.Trim().Replace(",", ""));
            }
            catch
            {
                throw new Exception($"[{para}]转换double类型失败！");
            }
        }

        public static bool JsonToBoolean(this string para)
        {
            try
            {
                return Convert.ToBoolean(para.Trim());
            }
            catch
            {
                throw new Exception($"[{para}]转换bool类型失败！");
            }
        }


    }
}
